$(document).ready(() => {
    let myOp = new classListOperation()
    myOp.bindClassList("#classlist")

    $("#btnClassInput").on("click", () => {
        layer.open({
            type: 2,
            title: '班级输入',
            shadeClose: true,
            shade: false,
            maxmin: true,
            area: ['800px', '600px'],
            content: './classinput.html'
        });
    })

})

function classListOperation() {
    this.bindClassList = function (tableid) {
        let myUrl = apiurl + "/sys/class"

        $.ajax({
            type: "GET",
            url: myUrl,
            dataType: "json",
            success: function (myData) {
                


                if ($.fn.DataTable.isDataTable(tableid) == false) {
                    $(tableid).dataTable({
                        "aaData": myData,
                        "aoColumns": [
                            {
                                "mData": "id",
                                "width": 40
                            },
                            {
                                "mData": "classname"
                            },
                            {
                                "mData": "graduated",
                                "width": 80
                            },
                            {
                                "mData": "graduationyear",
                                "width": 80
                            },
                            {
                                "mData": "id",
                                "render": function (data, type, row, meta) {
                                    if (type === "display") {
                                        data = "<button class='js-btn-type layui-btn layui-btn-xs layui-btn-normal' id='" + data + "'>修改</button>" +
                                            "<button class='js-btn-del layui-btn layui-btn-xs layui-btn-danger' id='" + data + "'>删除</button>"
                                    }
                                    return data;
                                },
                                "width": 90
                            }


                        ],
                        "language": {
                            url: "../../assets/js/Chinese.json"
                        },
                        "bPaginate": true,
                        "bInfo": true,
                        "filter": true,
                        "columnDefs": [
                            {
                                "targets": 0,
                                "className": "dt-body-center"
                            },
                            {
                                "targets": 2,
                                "className": "dt-body-center"
                            },
                            {
                                "targets": 3,
                                "className": "dt-body-center"
                            },
                            {
                                "targets": 4,
                                "className": "dt-body-center"
                            }
                        ]
                    })
                }
                else {
                    this.refreshTable(tableid, myUrl)
                }




            }
        }).done(function () {

        }).fail(function () {

        }, 2000);
    }

    this.refreshTable = function (tableid, url) {
        $.getJSON(url, null, function (json) {
            table = $(tableid).dataTable()
            oSettings = table.fnSettings()

            table.fnClearTable(this)

            var myTable = $(tableid).DataTable()
            myTable.clear().rows.add(json).draw()
        });
    }
}